﻿<Window x:Class="SwiftTailer.Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:converters="clr-namespace:SwiftTailer.Wpf.Converters"
        xmlns:viewModels="clr-namespace:SwiftTailer.Wpf.ViewModels"
        xmlns:wpf="clr-namespace:SwiftTailer.Wpf"
        xmlns:commands="clr-namespace:SwiftTailer.Wpf.Commands"
        xmlns:data="clr-namespace:SwiftTailer.Wpf.Data"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        mc:Ignorable="d" x:Name="ThisWindow"
        Title="SwiftTailer" Height="800" Width="1000" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <converters:InverseBoolConverter x:Key="InverseBoolConverter" />
        <converters:HighlightConverter x:Key="HighlightConverter"/>
        <converters:LineCountDisplayConverter x:Key="LineCountDisplayConverter" />
        <converters:EnumBoolConverter x:Key="EnumBoolConverter" />
        <converters:BooleanVisibilityConverter x:Key="BooleanVisibilityConverter" />
        <converters:StringNotNullOrEmptyConverter x:Key="StringNotNullOrEmptyConverter" />
        <Style TargetType="ListBoxItem" x:Key="LogLineStyle">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Header="View Selected" Command="commands:StaticCommands.OpenLogLineCommand" 
                                  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget}" />
                        <MenuItem Header="Pin this line" ToolTip="If pinned, the log line will be ommitted from trimming rules."
                                  Command="commands:StaticCommands.PinLogLineCommand" 
                                  CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
                        <MenuItem Header="Unpin this line"
                                  Command="commands:StaticCommands.UnpinLogLineCommand"
                                  CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="ToolTip" Value="{Binding LineContext}" />
            <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_DoubleClick" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Highlight, Converter={StaticResource HighlightConverter}}" Value="Find">
                    <Setter Property="Background" Value="Yellow" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Highlight, Converter={StaticResource HighlightConverter}}" Value="Error">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Highlight, Converter={StaticResource HighlightConverter}}" Value="General">
                    <Setter Property="Background" Value="LightGreen" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Highlight, Converter={StaticResource HighlightConverter}}" Value="Hide">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Pinned}" Value="True">
                    <Setter Property="FontStyle" Value="Italic" />
                    <Setter Property="FontWeight" Value="Black"/>
                    <Setter Property="Foreground" Value="DarkGreen" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="StatusBarItem">
            <Setter Property="Margin" Value="10 0"/>
        </Style>
        <Style TargetType="Button" x:Key="ToolbarIconStyle">
            <Setter Property="Width" Value="30" />
            <Setter Property="Height" Value="30"/>
            <Setter Property="Margin" Value="2 0" />
            <Setter Property="IsEnabled" Value="{Binding IsRunning, Converter={StaticResource InverseBoolConverter}}"/>
        </Style>
        <Style TargetType="Button" x:Key="TransportControlButtons">
            <Setter Property="Width" Value="25" />
            <Setter Property="Height" Value="25"/>
            <Setter Property="Margin" Value="2 0" />
        </Style>
    </Window.Resources>
    <Window.DataContext>
        <viewModels:MainViewModel />
    </Window.DataContext>
    
    <!-- GLOBAL INPUT BINDINGS -->
    <Window.InputBindings>
        <KeyBinding Gesture="CTRL+SPACE"
                    Command="{Binding ToggleTailingCommand}" />
        <KeyBinding Gesture="ALT+F"
                    Command="{Binding FollowTailToggleCommand}" />
        <KeyBinding Key="OemTilde" 
                    Command="{Binding ToggleSearchOptionsCommand}" />
    </Window.InputBindings>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        
        <!-- TOOL BARS -->
        <ToolBarTray Grid.Row="0">
            <ToolBar Band="0" BandIndex="0">
                <Button ToolTip="Start tailing (toggle ctrl-space)" x:Name="StartButton" Style="{StaticResource TransportControlButtons}" 
                        Command="{Binding StartTailingCommand}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="Assets/playback_play.png" />
                        <TextBlock Text="Start" />
                    </StackPanel>
                </Button>
                <Button ToolTip="Stop tailing (toggle ctrl-space)" x:Name="PauseButton" Style="{StaticResource TransportControlButtons}" 
                        Command="{Binding StopTailingCommand}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="Assets/playback_pause.png" />
                        <TextBlock  />
                    </StackPanel>
                </Button>
            </ToolBar>
            <ToolBar Band="0" BandIndex="1">
                <ComboBox Width="200" ItemsSource="{Binding Groups}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedGroup}" 
                          Margin="10 0 5 0" IsEnabled="{Binding IsRunning, Converter={StaticResource InverseBoolConverter}}" IsEditable="True" IsReadOnly="True" />
                <Button ToolTip="Add new group..."  Style="{StaticResource ToolbarIconStyle}" Command="{Binding AddGroupDialogCommand}">
                    <Image Source="Assets/folder_plus.png"/>
                </Button>
                <Button ToolTip="Delete this group..." Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.DeleteGroupCommand" CommandParameter="{Binding SelectedGroup}">
                    <Image Source="Assets/folder_minus.png" />
                </Button>
                <Button ToolTip="Rename this group..." Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.EditGroupCommand" CommandParameter="{Binding SelectedGroup}">
                    <Image Source="Assets/edit.png" />
                </Button>
                <Button ToolTip="Clone this group..." Style="{StaticResource ToolbarIconStyle}" 
                        Command="commands:StaticCommands.FeatureStubCommand" CommandParameter="Clone feature coming soon!">
                    <Image Source="Assets/copy.png"/>
                </Button>
                <Button ToolTip="Add log to this group..." Style="{StaticResource ToolbarIconStyle}" 
                    Command="{Binding OpenLogPickerDialogCommand}" CommandParameter="{Binding SelectedGroup}">
                    <Image Source="Assets/doc_plus.png"/>
                </Button>
                <Button ToolTip="Remove this log from group..." Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.RemoveLogCommand" CommandParameter="{Binding SelectedTail}">
                    <Image Source="Assets/doc_minus.png" />
                </Button>
                <Button ToolTip="Zip the current state of the group..." Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.ZipSelectedGroupCommand" CommandParameter="{Binding ElementName=ThisWindow, Path=DataContext}">
                    <Image Source="Assets/compress.png" />
                </Button>
                <Button ToolTip="Zip the current state of the group and email it..." Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.ZipGroupAndEmailCommand" CommandParameter="{Binding ElementName=ThisWindow, Path=DataContext}">
                    <Image Source="Assets/packages.png"/>
                </Button>
            </ToolBar>
            <ToolBar Band="0" BandIndex="2">
                <Button ToolTip="Gridulate" Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.FeatureStubCommand" CommandParameter="Gridulator coming soon!">
                    <Image Source="Assets/grid.png" />
                </Button>
            </ToolBar>
            <ToolBar Band="0" BandIndex="3">
                <Button ToolTip="Export Log Config" Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.ExportLogConfigCommand">
                    <Image Source="Assets/doc_export.png" />
                </Button>
                <Button ToolTip="Import Log Config" Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.ImportLogConfigCommand">
                    <Image Source="Assets/doc_import.png" />
                </Button>
                <Button ToolTip="Manually edit config" Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.OpenLogConfigInExplorerCommand">
                    <Image Source="Assets/edit.png" />
                </Button>
            </ToolBar>
            <ToolBar Band="0" BandIndex="4" Drop="UIElement_OnDrop" AllowDrop="True">
                <Button ToolTip="Open ad-hoc log" Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.OpenAdHocTailCommand">
                    <Image Source="Assets/open-message.png" />
                </Button>
                <Button ToolTip="Open Settings Dialog" Style="{StaticResource ToolbarIconStyle}"
                        Command="commands:StaticCommands.OpenSettingsCommand">
                    <Image Source="Assets/setting.png" />
                </Button>
            </ToolBar>
        </ToolBarTray>
        
        <!-- TAB CONTROL -->
        <TabControl ItemsSource="{Binding Tails}" Grid.Row="1" x:Name="SessionTabs" SelectedItem="{Binding SelectedTail}">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" Tag="{Binding Id}">
                        <TextBlock.ContextMenu>
                            <ContextMenu>
                                <ContextMenu.Resources>
                                    <Style TargetType="MenuItem">
                                        <Setter Property="IsCheckable" Value="False"/>
                                    </Style>
                                </ContextMenu.Resources>
                                <MenuItem Header="Open in another window" Command="commands:StaticCommands.OpenAdHocTailCommand"
                                          CommandParameter="{Binding FilePath}"/>
                                <MenuItem Header="Edit log info" Command="commands:StaticCommands.OpenEditLogWindowCommand"
                                          CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
                                <MenuItem Header="Open in explorer" Command="{Binding OpenMenuItemInExplorerCommand}" 
                                          CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                    </TextBlock>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        
                        <!-- LOG LINES -->
                        <ListBox x:Name="LogList" ItemsSource="{Binding LogLines, UpdateSourceTrigger=PropertyChanged}" Grid.Row="0" SelectionMode="Extended" 
                                 ItemContainerStyle="{StaticResource LogLineStyle}" FontFamily="{x:Static wpf:Settings.LogWindowFontFamily}"
                                 SelectedIndex="{Binding SelectedLine}" SelectionChanged="LogList_SelectionChanged">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Vertical">
                                        <TextBlock Text="{Binding Content}" ToolTipService.InitialShowDelay="1000" ToolTipService.ShowDuration="20000" 
                                                   ToolTipService.HasDropShadow="True" ToolTip="{Binding LineContext}">
                                        </TextBlock>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                        
                        <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Right" Margin="5 5 5 0">
                            <CheckBox Content="Follow Tail" IsChecked="{Binding FollowTail}" ToolTip="Selection will stay pinned to the last line. (alt-F)" />
                        </StackPanel>
                        
                        <!-- SEARCH PANEL -->
                        <Expander Grid.Row="2" Margin="5 10 2 5" Style="{DynamicResource ExpanderStretchHeader}" ToolTip="Search options (~)" Name="SearchOptions" IsExpanded="{Binding ShowSearchOptions}">
                            <Expander.Header>
                                <DockPanel LastChildFill="True">
                                    <Label Content="Search:"/>
                                    <TextBox x:Name="LogFilter" HorizontalAlignment="Stretch" BorderBrush="Gray" VerticalContentAlignment="Center"
                                        Text="{Binding SearchOptions.SearchPhrase, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Delay=500}"/>
                                </DockPanel>
                            </Expander.Header>
                            <StackPanel Orientation="Horizontal">
                                <GroupBox Header="Case Sensitivity" Padding="5 5 5 0" Margin="2"
                                          ToolTip="Whether or not the search will use Ordinal matching rules.">
                                    <StackPanel Orientation="Vertical">
                                        <RadioButton GroupName="CaseSensitivity" Content="On" IsChecked="{Binding SearchOptions.CaseSensitive, Mode=TwoWay}"/>
                                        <RadioButton GroupName="CaseSensitivity" Content="Off" IsChecked="{Binding SearchOptions.CaseSensitive, Converter={StaticResource InverseBoolConverter}, Mode=TwoWay}" />
                                    </StackPanel>
                                </GroupBox>

                                <GroupBox Header="Search Mode" Padding="5 5 5 0" Margin="2"
                                          ToolTip="How the search will return hits. Find will highlight hits, Filter will disclude all non-hits.">
                                    <StackPanel Orientation="Vertical">
                                        <RadioButton GroupName="SearchMode" Name="FindModeRdo"  Content="Find" IsChecked="{Binding SearchOptions.SearchMode, Mode=TwoWay, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Find}" />
                                        <RadioButton GroupName="SearchMode" Name="FilterModeRdo" Content="Filter" IsChecked="{Binding SearchOptions.SearchMode, Mode=TwoWay, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Filter}"/>
                                    </StackPanel>
                                </GroupBox>

                                <GroupBox Header="Lines of Context" Visibility="{Binding ElementName=FilterModeRdo, Path=IsChecked, Converter={StaticResource BooleanVisibilityConverter}}"
                                          ToolTip="Experimental. Each line will contain a tool tip with the filter hit along with x lines of context, before and/or after.">
                                    <StackPanel Orientation="Vertical">
                                        <TextBlock Text="Head"/>
                                        <xctk:IntegerUpDown Value="{Binding SearchOptions.ContextHeadSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Delay=1000}" Minimum="0" />
                                        <TextBlock Text="Tail"/>
                                        <xctk:IntegerUpDown Value="{Binding SearchOptions.ContextTailSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Delay=1000}" Minimum="0" />
                                    </StackPanel>
                                </GroupBox>
                                
                                <GroupBox Header="Phrase Type" Padding="5 5 5 0" Margin="2"
                                          ToolTip="How the search phrase will be treated">
                                    <StackPanel Orientation="Vertical">
                                        <RadioButton GroupName="PhraseType" Content="Literal" IsChecked="{Binding SearchOptions.PhraseType, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Literal, Mode=TwoWay}" />
                                        <RadioButton GroupName="PhraseType" Content="Regex" IsChecked="{Binding SearchOptions.PhraseType, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Regex, Mode=TwoWay}"/>
                                    </StackPanel>
                                </GroupBox>

                                <GroupBox Header="Error keywords" Padding="5 5 5 0" Margin="2" Width="150" Height="100"
                                          ToolTip="Comma delimited set of words that will be highlighted in red">
                                    <Grid>
                                        <TextBox Text="{Binding SearchOptions.ErrorPhrases, Mode=TwoWay, Delay=500, UpdateSourceTrigger=PropertyChanged}" />
                                    </Grid>
                                </GroupBox>

                                <GroupBox Header="General keywords" Padding="5 5 5 0" Margin="2" Width="150" Height="100"
                                          ToolTip="Comma delimited set of words that will be highlighted in green">
                                    <Grid>
                                        <TextBox Text="{Binding SearchOptions.GeneralPhrases, Mode=TwoWay, Delay=500, UpdateSourceTrigger=PropertyChanged}" />
                                    </Grid>
                                </GroupBox>
                            </StackPanel>
                        </Expander>
                    </Grid>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
        
        <!-- STATUS BAR -->
        <StatusBar Grid.Row="4" MinHeight="20">
            <StatusBarItem Content="{Binding Status, FallbackValue=Idle}" HorizontalAlignment="Left" />
            <StatusBarItem Content="{Binding SelectedGroup.Name, FallbackValue='Group 1'}" HorizontalAlignment="Left" />
            <StatusBarItem Content="{Binding SelectedTail.LineCount, Converter={StaticResource LineCountDisplayConverter}}" />
            <StatusBarItem HorizontalAlignment="Right">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" MaxWidth="300" />
                        <ColumnDefinition Width="100" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding ProgressText, FallbackValue='Processing c:\\MyDocuments\\MyFilt.txt...'}" Margin="5 0" />
                    <ProgressBar Value="{Binding ProgressBarValue}" Maximum="100" Width="100" Height="15" x:Name="ProgressBar" Grid.Column="1" />
                </Grid>
            </StatusBarItem>
        </StatusBar>
    </Grid>
</Window>
